const Service = require('egg').Service

class UserService extends Service {
  // create======================================================================================================>
  async create(payload) {
    const { ctx, service } = this
    // const role = await service.role.show(payload.role)
    const role = await ctx.model.Role.findOne({access : payload.access})
    if (!role) {
      ctx.throw(404, 'role is not found')
    }
    payload.role = role._id
    payload.password = await this.ctx.genHash(payload.password)
    return ctx.model.User.create(payload)
  }

  // destroy======================================================================================================>  
  async destroy(_id) {
    const { ctx, service } = this
    const user = await ctx.service.user.find(_id)
    if (!user) {
      ctx.throw(404, 'user not found')
    }
    return ctx.model.User.findByIdAndRemove(_id)
  }

  // update======================================================================================================>
  async update(_id, payload) {
    const { ctx, service } = this
    const user = await ctx.service.user.find(_id)
    if (!user) {
      ctx.throw(404, 'user not found')
    }
    // console.log(`000${payload.password}000`)
    if (payload.password) {
      payload.password = await this.ctx.genHash(payload.password)
    }else{
      delete payload.password
    }
    
    const role = await ctx.model.Role.findOne({access : payload.access})
    if (!role) {
      ctx.throw(404, 'role not found')
    }
    if (role._id !== payload.role._id) {
      payload.role = role._id
    }

    // console.log(payload)
    return ctx.model.User.findByIdAndUpdate(_id, payload)
  }

  // show======================================================================================================>
  async show(_id) {
    const user = await this.ctx.service.user.find(_id)
    if (!user) {
      this.ctx.throw(404, 'user not found')
    }
    return this.ctx.model.User.findById(_id).populate('role')
  }

  // index======================================================================================================>
  async index(payload, id = undefined) {
    // const { currentPage, pageSize, isPaging, search } = payload
    // let res = []
    // let count = 0
    // let skip = ((Number(currentPage)) - 1) * Number(pageSize || 10)

    // // 对象映射
    // let populate = ['role']
    // // 

    // if (isPaging) {
    //   if (search) {
    //     res = await this.ctx.model.User.find({ mobile: { $regex: search } }).populate('role').skip(skip).limit(Number(pageSize)).sort({ createdAt: -1 }).exec()
    //     count = res.length
    //   } else {
    //     res = await this.ctx.model.User.find({}).populate('role').skip(skip).limit(Number(pageSize)).sort({ createdAt: -1 }).exec()
    //     count = await this.ctx.model.User.count({}).exec()
    //   }
    // } else {
    //   if (search) {
    //     res = await this.ctx.model.User.find({ mobile: { $regex: search } }).populate('role').sort({ createdAt: -1 }).exec()
    //     count = res.length
    //   } else {
    //     res = await this.ctx.model.User.find({}).populate('role').sort({ createdAt: -1 }).exec()
    //     count = await this.ctx.model.User.count({}).exec()
    //   }
    // }
    // // 整理数据源 -> Ant Design Pro
    // let data = res.map((e, i) => {
    //   const jsonObject = Object.assign({}, e._doc)
    //   jsonObject.key = i
    //   jsonObject.password = 'Are you ok?'
    //   jsonObject.createdAt = this.ctx.helper.formatTime(e.createdAt)
    //   return jsonObject
    // })

    // return { count: count, list: data, pageSize: Number(pageSize), currentPage: Number(currentPage) }

    let { currentPage, pageSize, search } = payload

    // 分页
    pageSize = Number(pageSize || 10)
    currentPage = Number(currentPage || 1)
    const skip = (currentPage - 1) * pageSize

    // 排序
    const sort = { createdAt: -1 }

    // 对象映射
    let populate = ['role']
    // .populate(populate)

    // 查询参数
    let querys = {}
    if (search) {
      querys.name = { $regex: search }
    }
    if (id) {
      querys.user = id
    }

    const res = await this.ctx.model.User.find(querys).skip(skip).limit(pageSize).sort(sort).populate(populate).exec()
    const count = await this.ctx.model.User.countDocuments(querys).exec()

    // 整理数据源 -> Ant Design Pro
    const list = res.map((e, i) => {
      const jsonObject = Object.assign({}, e._doc)
      // jsonObject.index = i
      jsonObject.createdAt = e.createdAt.getTime()
      jsonObject.access = e.role && e.role.access
      return jsonObject
    })

    return { count, list, pageSize, currentPage }
  }


  async removes(payload) {
    return this.ctx.model.User.remove({ _id: { $in: payload } })
  }

  // Commons======================================================================================================>
  async findByMobile(mobile) {
    return this.ctx.model.User.findOne({ mobile: mobile })
  }

  async find(id) {
    return this.ctx.model.User.findById(id)
  }

  async findByIdAndUpdate(id, values) {
    return this.ctx.model.User.findByIdAndUpdate(id, values)
  }



}


module.exports = UserService